
const Koa = require('koa');
const app = new Koa();

//检测文件 更新插件
const fs = require('fs');// 引入fs 模块
let limit = true;
const watch = require('watch');
const chokidar = require('chokidar')

// response 
app.use(ctx => {
  ctx.body = 'Hello Koa';
});
 
// app.listen(3000);
var server = require('http').createServer(app.callback(function(){
    console.log("callback")
}));

var io = require('socket.io')(server);

io.on('connection', function(){ 
    console.log("connect");
    thcat_watch(function(){
        io.send("reload");
    });
  
    io.send("first_connect ok！");
  
});

io.on("message",function(e){
    console.log("message");
    console.log(e);
})


function thcat_watch(cb) {
    // var watcher = chokidar.watch('file, dir, or glob', {
    //     ignored: /[\/\\]\./, persistent: true
    // });
    // var watcher = chokidar.watch('./views/demo');
   var watcher =  chokidar.watch('.', {ignored: /node_modules/}).on('all', (event, path) => {
        console.log(event, path);
      });
    var log = console.log.bind(console);
    console.log(new Date());
    watcher
        .on('add', function (path) {
            log('File', path, 'has been added');
        })
        .on('addDir', function (path) {
            log('Directory', path, 'has been added');
        })
        .on('change', function (path) {
            // if(~path.indexOf(".idea")){
            //         return;
            //     }
            console.log("change")
            log('File', path, 'has been changed');
        })
        .on('unlink', function (path) {
            log('File', path, 'has been removed');
        })
        .on('unlinkDir', function (path) {
            log('Directory', path, 'has been removed');
        })
        .on('error', function (error) {
            log('Error happened', error);
        })
        .on('ready', function () {
            log('Initial scan complete. Ready for changes.');
        })
        .on('raw', function (event, path, details) {
            console.log(path);
            if (( ~path.indexOf("css") || ~path.indexOf("html") || ~path.indexOf("js") ) && limit) {
                console.log(new Date());
                //不知为何 会自己 触发4次 加个限制

                limit = false;
                setTimeout(function () {
                    limit = true
                    cb();
                    return;
                    if(ws.readyState !== 1){
                        // init_handle();
                        return;
                    }
                        ws.send("wx",function ack (error) {
                            //  如果未定义错误，则发送已完成，否则发生错误
                            //  对象将指示失败的内容。
                            console.log("page___");
                            console.log(error);
                            console.log("socket.readyState ")
                            console.log(ws.readyState);
                            if(ws.readyState == 3 ){
                                console.log("3333333333");
                                // ws.close()
                                // ws.open()
                                console.log("readYstate________________________-")
                                console.log(ws.readyState)
                            }
                            if(!error){

                            }else{
                                var  err= JSON.stringify(error);
                                if(~err.indexOf("Error")){
                                    init_wss()
                                }

                                console.log(!! err.indexOf("Error"))
                            }

                           // init_wss();
                        })
                }, 500);
            }
            // log('Raw event info:', event, path, details);
        });
}

server.listen(7000);